const fs = require("fs")
const yargs = require("yargs")
const uuid = require("uuid")
var http = require("http");

var Database = {};

var DynamicDatabase = {
    init: () => {
        let files = fs.readdirSync('./data');
        files.forEach(file => {
            if (file.includes(".js")) {
                let colName = file.replace('.js', '');
                let colData = fs.readFileSync(`./data/${file}`);
                eval(`Database[colName] = [${colData}]`)
            }
        })
    },
    createCollection: (name) => {
        Database[name] = [];
    },
    create: (collection, data) => {
        eval(`var t = ${data}`)
        fs.appendFileSync(`./data/${collection}.js`, `${data},`);
        Database[collection].push(t);
    },
    find: (collection, condition) => {
        return Database[collection].filter(condition);
    }
};
DynamicDatabase.init();
// DynamicDatabase.createCollection("users");
DynamicDatabase.create("users", `{
    id: 1,
    user: "tt1",
    age: ()=>{ return 1 }
}`);
DynamicDatabase.create("users", `{
    id: 2,
    user: "tt2"
}`);
DynamicDatabase.create("users", `{
    id: 3,
    user: "tt3"
}`);
console.log(Database)
    // var result = DynamicDatabase.find("users", (v) => {
    //     return v.id == 1
    // });

// http.createServer(function(req, res) {
//     res.writeHead(200, {
//         "content-type": "application/json"
//     });
//     var query = req.url.substr(req.url.indexOf('?') + 1, req.url.length)
//     console.log(query)
//     var tResult = data.filter(v => {
//         return v.id == query
//     })
//     console.log(tResult)
//     var result = tResult[0].dob()
//     res.write(JSON.stringify(result));
//     res.end();
// }).listen(3000);